package com.study.hot.jetpack.demo_words;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import androidx.recyclerview.widget.RecyclerView;

import com.study.hot.R;
import com.study.hot.jetpack.demo_words.db.Words;
import com.study.hot.jetpack.demo_words.ui.WordsAdapter;
import com.study.hot.jetpack.demo_words.viewmodel.WordsViewModel;

/**
 * <pre>
 *     author : slx
 *     time   : 2020/03/31
 *     desc   :
 * </pre>
 */
public class WordsActivity extends AppCompatActivity {


    private NavController navController;

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_words_navigation);
        navController = Navigation.findNavController(findViewById(R.id.wordsFg));
        NavigationUI.setupActionBarWithNavController(this, navController);
    }

    @Override public boolean onSupportNavigateUp() {
        navController.navigateUp();
        return super.onSupportNavigateUp();
    }

    //在Activity中处理，现在改用Navigation
    private WordsViewModel wordsViewModel;
    private RecyclerView rvWords;

    private void useInActivity() {
        setContentView(R.layout.activity_words);
        wordsViewModel = new ViewModelProvider(this).get(WordsViewModel.class);
        WordsAdapter wordsAdapter = new WordsAdapter(wordsViewModel,true);
        rvWords = findViewById(R.id.wordsRv);
        rvWords.setAdapter(wordsAdapter);
        wordsViewModel.getAllWords().observe(this, words -> {
            //此处使用LiveData，实体中添加了boolean变量用于判断中文是否显示，局部刷新
            //为了防止局部刷新导致全局刷新卡顿UI此处需要添加判断，如果不是数据改变则不设置新数据
            int oldCount = wordsAdapter.getItemCount();
            if (oldCount != words.size()) {
                wordsAdapter.setWordsList(words);
            }
            wordsAdapter.notifyDataSetChanged();
        });

        findViewById(R.id.insert).setOnClickListener(v -> {
            Words[] wordsArray = {
                    new Words("你好", "hello"),
                    new Words("世界", "word"),
                    new Words("黄色", "yellow"),
                    new Words("蓝色", "blue"),
                    new Words("红色", "red"),
            };
            wordsViewModel.insertWords(wordsArray);
        });

        findViewById(R.id.delete).setOnClickListener(v -> {
            Words words1 = new Words();
            Words words2 = new Words();
            words1.setWordsId(1);
            words1.setWordsId(2);
            wordsViewModel.deleteWords(words1, words2);
        });
        findViewById(R.id.update).setOnClickListener(v -> {
            Words words1 = new Words("大的", "big");
            words1.setWordsId(1);
            wordsViewModel.deleteWords(words1, words1);
        });

        findViewById(R.id.clear).setOnClickListener(v -> wordsViewModel.deleteAllWords());
    }
}
